//封装优先级队列，priority越小，优先级越高
function PriorityQueue () {
  //存放每一个插入元素QueueElement
  this.items = [];
  //实现插入操作
  PriorityQueue.prototype.enqueue = function (element) {
    if (this.items.length === 0) {
      this.items.push(element);
    } else {
      var flag = false; //记录元素是否插入，没有就插入到items最后
      for (var i = 0; i < this.items.length; i++) {
        if (element < this.items[i]) {
          this.items.splice(i, 0, element);
          flag = true;
          break;
        }
      }
      if (!flag) {	//没有就插入到items最后
        this.items.push(element);
      }
    }
  }

  //2.将元素从队列中移除
  PriorityQueue.prototype.dequeue = function () {
    return this.items.shift();
  }
  //3.查看队列中第一个元素
  PriorityQueue.prototype.front = function () {
    return this.items[0];
  }
  //4.查看队列是否为空
  PriorityQueue.prototype.isEmpty = function () {
    return this.items.length === 0;
  }
  //5.查看队列中元素的个数
  PriorityQueue.prototype.size = function () {
    return this.items.length;
  }
  //6.toString
  PriorityQueue.prototype.toString = function () {
    var result = '';
    for (var i = 0; i < this.items.length; i++) {
      result += this.items[i] + ' ';
    }
    return result;
  }
}
